package com.simple.util;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ExecutionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * 操作工作流工具类
 *
 * @author hehongfa
 * @date 2021-12-29 10:58:56
 */
@Component
public class ActivitiUtil {
    @Autowired
    private HistoryService historyService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private RepositoryService repositoryService;

    /**
     * 启动一个流程
     *
     * @param processDefinitionId
     */
    public ProcessInstance startWorkflow(String processDefinitionId) {
        return runtimeService.startProcessInstanceById(processDefinitionId);
    }

    public void stopWorkflow() {

    }

    /**
     * 删除正在运行的流程实例
     *
     * @author hehongfa
     * @date 2021-12-29 12:06:56
     */
    public void deleteWorkflow(String processInstanceId, String deleteReason) {
        runtimeService.deleteProcessInstance(processInstanceId, deleteReason);
    }

    /**
     * 审批任务
     *
     * @param taskId
     * @param variables
     */
    public void complete(String taskId, Map<String, Object> variables) {
        taskService.complete(taskId, variables);
    }

    /**
     * 部署文件
     *
     * @param resource
     * @param name
     */
    public void deploy(String resource, String name) {

        // 部署对象
        DeploymentBuilder createDeployment = repositoryService.createDeployment();
        // 上传资源
        InputStream inputStream = this.getClass().getResourceAsStream(resource + ".bpmn");
        InputStream inputStream2 = this.getClass().getResourceAsStream(resource + ".png");
        createDeployment.addInputStream(resource + ".bpmn", inputStream);
        createDeployment.addInputStream(resource + ".png", inputStream2);
        createDeployment.name(name);
        // 部署
        createDeployment.deploy();
    }

    /**
     * 启动实例带流程变量
     */
    public ProcessInstance start(String key, Map<String, Object> variables) {
        // 启动
        // 设置一下变量
        ProcessInstance startProcessInstanceByKey = runtimeService.startProcessInstanceByKey(key, variables);
        return startProcessInstanceByKey;
    }

    /**
     * 启动实例带流程变量
     */
    public ProcessInstance start(String key) {
        // 启动
        // 设置一下变量
        ProcessInstance startProcessInstanceByKey = runtimeService.startProcessInstanceByKey(key);
        return startProcessInstanceByKey;
    }

    /**
     * 完成任务 带流程变量 方便流转到下以个节点
     */
    public void completeTask(String taskId, Map<String, Object> variables) throws Exception {
        taskService.complete(taskId, variables);
    }

    /**
     * 完成任务
     */
    public void completeTask(String taskId) throws Exception {
        taskService.complete(taskId);
    }

    /**
     * 完成任务 实例id 和 人的名称
     */
    public void completeTaskByPIdAndName(String processInstanceId, String name) throws Exception {
        // 获取查询对象
        TaskQuery createTaskQuery = taskService.createTaskQuery();
        // 获取任务id
        Task task = createTaskQuery.processInstanceId(processInstanceId).taskAssignee(name).singleResult();
        // 完成
        taskService.complete(task.getId());
    }

    /**
     * 完成任务 实例id 和 人的名称
     */
    public void completeTaskByPIdAndName(String processInstanceId, String name, Map<String, Object> variables) throws Exception {
        // 获取查询对象
        TaskQuery createTaskQuery = taskService.createTaskQuery();
        // 获取任务id
        Task task = createTaskQuery.processInstanceId(processInstanceId).taskAssignee(name).singleResult();
        // 完成
        taskService.complete(task.getId(), variables);
    }

    /**
     * 查询某个人的任务列表
     */
    public List<Task> queryTaskList(String name) throws Exception {
        // 获取查询对象
        TaskQuery createTaskQuery = taskService.createTaskQuery();
        // 设置查询条件
        createTaskQuery.taskAssignee(name);
        // 查询列表
        List<Task> list = createTaskQuery.list();
        return list;
    }

    /**
     * 查询某个人的当前任务
     */
    public Task queryTaskByName(String processInstanceId, String name) throws Exception {
        // 获取查询对象
        TaskQuery createTaskQuery = taskService.createTaskQuery();
        // 设置查询条件
        createTaskQuery.taskAssignee(name);
        createTaskQuery.processInstanceId(processInstanceId);
        //
        Task task = createTaskQuery.singleResult();
        return task;
    }

    /**
     * 查询当前执行对象(当前活动节点)
     */
    public Execution queryActivityNode(String processInstanceId, String actId) throws Exception {
        // 获取查询对象
        ExecutionQuery createExecutionQuery = runtimeService.createExecutionQuery();
        // 设置查询条件
        Execution execution = createExecutionQuery.processInstanceId(processInstanceId).activityId(actId).singleResult();
        // 返回
        return execution;
    }

    // 查询流程是否结束
    public void isEnd(String pid) {
        // 查询对象
        ProcessInstanceQuery createProcessInstanceQuery = runtimeService.createProcessInstanceQuery();
        // 根据id
        ProcessInstance singleResult = createProcessInstanceQuery.processInstanceId(pid).singleResult();
        // 如果singleResult找到说明流程还没有结束
        if (singleResult != null) {
            System.out.println("流程未结束或不存在");
        } else {
            System.out.println("流程结束");
        }
    }
}